package com.hoffm59.facebookcontactsync;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.facebook.FacebookException;
import com.facebook.widget.FriendPickerFragment;
import com.facebook.widget.PickerFragment;

/**
 * @author HOFFM59
 */
public class PickerActivity
        extends FragmentActivity {

    public static final Uri FRIEND_PICKER = Uri.parse("picker://friend");

    private FriendPickerFragment friendPickerFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.pickers);

        final Bundle args = this.getIntent().getExtras();
        final FragmentManager manager = this.getSupportFragmentManager();
        Fragment fragmentToShow = null;
        final Uri intentUri = this.getIntent().getData();

        if (PickerActivity.FRIEND_PICKER.equals(intentUri)) {
            if (savedInstanceState == null) {
                this.friendPickerFragment = new FriendPickerFragment(args);
            } else {
                this.friendPickerFragment = (FriendPickerFragment) manager.findFragmentById(R.id.picker_fragment);
            }

            final List<String> extraFields = new ArrayList<String>();
            extraFields.add("id");
            extraFields.add("name");
            extraFields.add("link");
            this.friendPickerFragment.setExtraFields(extraFields);

            // Set the listener to handle errors
            this.friendPickerFragment.setOnErrorListener(new PickerFragment.OnErrorListener() {
                @Override
                public void onError(PickerFragment<?> fragment, FacebookException error) {

                    PickerActivity.this.onError(error);
                }
            });
            // Set the listener to handle button clicks
            this.friendPickerFragment.setOnDoneButtonClickedListener(new PickerFragment.OnDoneButtonClickedListener() {
                @Override
                public void onDoneButtonClicked(PickerFragment<?> fragment) {

                    PickerActivity.this.finishActivity();
                }
            });
            fragmentToShow = this.friendPickerFragment;

        } else {
            // Nothing to do, finish
            this.setResult(Activity.RESULT_CANCELED);
            this.finish();
            return;
        }

        manager.beginTransaction().replace(R.id.picker_fragment, fragmentToShow).commit();
    }

    @Override
    protected void onStart() {

        super.onStart();
        if (PickerActivity.FRIEND_PICKER.equals(this.getIntent().getData())) {
            try {
                this.friendPickerFragment.loadData(false);
            } catch (final Exception ex) {
                this.onError(ex);
            }
        }
    }

    private void onError(Exception error) {

        this.onError(error.getLocalizedMessage(), false);
    }

    private void onError(String error, final boolean finishActivity) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.error_dialog_title).setMessage(error)
                .setPositiveButton(R.string.error_dialog_button_text, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        if (finishActivity) {
                            PickerActivity.this.finishActivity();
                        }
                    }
                });
        builder.show();
    }

    private void finishActivity() {

        final PickerApplication app = (PickerApplication) this.getApplication();
        if (PickerActivity.FRIEND_PICKER.equals(this.getIntent().getData())) {
            if (this.friendPickerFragment != null) {
                app.setSelectedUsers(this.friendPickerFragment.getSelection());
            }
        }
        this.setResult(Activity.RESULT_OK, null);
        this.finish();
    }
}
